package com.pgvector;

import java.io.Serializable;
import java.util.Arrays;

/**
 * PGvector的测试替代类，用于测试环境
 */
public class PGvector implements Serializable {
    private static final long serialVersionUID = 1L;
    
    private float[] vector;

    /**
     * 创建一个PGvector对象
     * @param vector 向量数据
     */
    public PGvector(float[] vector) {
        this.vector = vector;
    }

    /**
     * 获取向量数据
     *
     * @return 向量数据
     */
    public float[] getVector() {
        return vector;
    }

    /**
     * 设置向量数据
     * @param vector 向量数据
     */
    public void setVector(float[] vector) {
        this.vector = vector;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PGvector pgVector = (PGvector) o;
        return Arrays.equals(vector, pgVector.vector);
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(vector);
    }

    @Override
    public String toString() {
        return "PGvector{" +
                "vector=" + Arrays.toString(vector) +
                '}';
    }

    /**
     * 计算与另一个向量的欧几里得距离
     * @param other 另一个向量
     * @return 欧几里得距离
     */
    public double distanceEuclidean(PGvector other) {
        if (vector.length != other.vector.length) {
            throw new IllegalArgumentException("Vectors must have the same dimension");
        }

        double sum = 0.0;
        for (int i = 0; i < vector.length; i++) {
            double diff = vector[i] - other.vector[i];
            sum += diff * diff;
        }
        return Math.sqrt(sum);
    }

    /**
     * 计算与另一个向量的余弦相似度
     * @param other 另一个向量
     * @return 余弦相似度
     */
    public double cosineSimilarity(PGvector other) {
        if (vector.length != other.vector.length) {
            throw new IllegalArgumentException("Vectors must have the same dimension");
        }

        double dotProduct = 0.0;
        double normA = 0.0;
        double normB = 0.0;
        for (int i = 0; i < vector.length; i++) {
            dotProduct += vector[i] * other.vector[i];
            normA += vector[i] * vector[i];
            normB += other.vector[i] * other.vector[i];
        }
        
        if (normA == 0 || normB == 0) {
            return 0;
        }
        
        return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
    }
} 